package ca.liu.scaffoldingmvctest.web;

import ca.liu.scaffoldingmvctest.dao.StLogDAO;
import ca.liu.scaffoldingmvctest.dao.StUserDAO;

import ca.liu.scaffoldingmvctest.domain.StLog;
import ca.liu.scaffoldingmvctest.domain.StUser;

import ca.liu.scaffoldingmvctest.service.StUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for StUser entities
 * 
 */

@Controller("StUserController")
public class StUserController {

	/**
	 * DAO injected by Spring that manages StLog entities
	 * 
	 */
	@Autowired
	private StLogDAO stLogDAO;

	/**
	 * DAO injected by Spring that manages StUser entities
	 * 
	 */
	@Autowired
	private StUserDAO stUserDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for StUser entities
	 * 
	 */
	@Autowired
	private StUserService stUserService;

	/**
	 * Create a new StUser entity
	 * 
	 */
	@RequestMapping("/newStUser")
	public ModelAndView newStUser() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stuser", new StUser());
		mav.addObject("newFlag", true);
		mav.setViewName("stuser/editStUser.jsp");

		return mav;
	}

	/**
	 * Delete an existing StUser entity
	 * 
	 */
	@RequestMapping("/deleteStUser")
	public String deleteStUser(@RequestParam Integer idKey) {
		StUser stuser = stUserDAO.findStUserByPrimaryKey(idKey);
		stUserService.deleteStUser(stuser);
		return "forward:/indexStUser";
	}

	/**
	 * Show all StLog entities by StUser
	 * 
	 */
	@RequestMapping("/listStUserStLogs")
	public ModelAndView listStUserStLogs(@RequestParam Integer idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stuser", stUserDAO.findStUserByPrimaryKey(idKey));
		mav.setViewName("stuser/stlogs/listStLogs.jsp");

		return mav;
	}

	/**
	 * Delete an existing StLog entity
	 * 
	 */
	@RequestMapping("/deleteStUserStLogs")
	public ModelAndView deleteStUserStLogs(@RequestParam Integer stuser_id, @RequestParam Integer related_stlogs_id) {
		ModelAndView mav = new ModelAndView();

		StUser stuser = stUserService.deleteStUserStLogs(stuser_id, related_stlogs_id);

		mav.addObject("stuser_id", stuser_id);
		mav.addObject("stuser", stuser);
		mav.setViewName("stuser/viewStUser.jsp");

		return mav;
	}

	/**
	 * Select an existing StUser entity
	 * 
	 */
	@RequestMapping("/selectStUser")
	public ModelAndView selectStUser(@RequestParam Integer idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stuser", stUserDAO.findStUserByPrimaryKey(idKey));
		mav.setViewName("stuser/viewStUser.jsp");

		return mav;
	}

	/**
	 * Edit an existing StUser entity
	 * 
	 */
	@RequestMapping("/editStUser")
	public ModelAndView editStUser(@RequestParam Integer idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stuser", stUserDAO.findStUserByPrimaryKey(idKey));
		mav.setViewName("stuser/editStUser.jsp");

		return mav;
	}

	/**
	 * View an existing StLog entity
	 * 
	 */
	@RequestMapping("/selectStUserStLogs")
	public ModelAndView selectStUserStLogs(@RequestParam Integer stuser_id, @RequestParam Integer stlogs_id) {
		StLog stlog = stLogDAO.findStLogByPrimaryKey(stlogs_id, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("stuser_id", stuser_id);
		mav.addObject("stlog", stlog);
		mav.setViewName("stuser/stlogs/viewStLogs.jsp");

		return mav;
	}

	/**
	 * Save an existing StUser entity
	 * 
	 */
	@RequestMapping("/saveStUser")
	public String saveStUser(@ModelAttribute StUser stuser) {
		stUserService.saveStUser(stuser);
		return "forward:/indexStUser";
	}

	/**
	 * Show all StUser entities
	 * 
	 */
	@RequestMapping("/indexStUser")
	public ModelAndView listStUsers() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stusers", stUserService.loadStUsers());

		mav.setViewName("stuser/listStUsers.jsp");

		return mav;
	}

	/**
	 * Select the StUser entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteStUser")
	public ModelAndView confirmDeleteStUser(@RequestParam Integer idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stuser", stUserDAO.findStUserByPrimaryKey(idKey));
		mav.setViewName("stuser/deleteStUser.jsp");

		return mav;
	}

	/**
	 */
	@RequestMapping("/stuserController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Create a new StLog entity
	 * 
	 */
	@RequestMapping("/newStUserStLogs")
	public ModelAndView newStUserStLogs(@RequestParam Integer stuser_id) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("stuser_id", stuser_id);
		mav.addObject("stlog", new StLog());
		mav.addObject("newFlag", true);
		mav.setViewName("stuser/stlogs/editStLogs.jsp");

		return mav;
	}

	/**
	 * Select the child StLog entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteStUserStLogs")
	public ModelAndView confirmDeleteStUserStLogs(@RequestParam Integer stuser_id, @RequestParam Integer related_stlogs_id) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("stlog", stLogDAO.findStLogByPrimaryKey(related_stlogs_id));
		mav.addObject("stuser_id", stuser_id);
		mav.setViewName("stuser/stlogs/deleteStLogs.jsp");

		return mav;
	}

	/**
	 * Edit an existing StLog entity
	 * 
	 */
	@RequestMapping("/editStUserStLogs")
	public ModelAndView editStUserStLogs(@RequestParam Integer stuser_id, @RequestParam Integer stlogs_id) {
		StLog stlog = stLogDAO.findStLogByPrimaryKey(stlogs_id, -1, -1);

		ModelAndView mav = new ModelAndView();
		mav.addObject("stuser_id", stuser_id);
		mav.addObject("stlog", stlog);
		mav.setViewName("stuser/stlogs/editStLogs.jsp");

		return mav;
	}

	/**
	 * Save an existing StLog entity
	 * 
	 */
	@RequestMapping("/saveStUserStLogs")
	public ModelAndView saveStUserStLogs(@RequestParam Integer stuser_id, @ModelAttribute StLog stlogs) {
		StUser parent_stuser = stUserService.saveStUserStLogs(stuser_id, stlogs);

		ModelAndView mav = new ModelAndView();
		mav.addObject("stuser_id", stuser_id);
		mav.addObject("stuser", parent_stuser);
		mav.setViewName("stuser/viewStUser.jsp");

		return mav;
	}

	/**
	 * Entry point to show all StUser entities
	 * 
	 */
	public String indexStUser() {
		return "redirect:/indexStUser";
	}
}